<?php
/**
 * Created by PhpStorm.
 * User: zhixuedong
 * Date: 2023-06-14
 * Time: 09:47
 */

namespace app\project\controller;

use app\common\controller\Common;
use app\project\service\PublicService;
use think\Controller;
use think\Db;
use think\Exception;
use think\Session;

class Employ extends Controller
{
    //审核操作
    public function autoSave(){
        try {
            $data = $_POST;
            $where['id'] = $data['id'];
            $detail = Db::name("project_pro_figure")->where($where)->find();
            if(!empty($detail['status'])){
                throw new Exception("已审核，请勿重复操作");
            }
            if(empty($data['status'])){
                throw new Exception("请选择审核操作");
            }

            if($data['status']==1 & empty($data['accountId'])){
                throw new Exception("请添加协调人员");
            }

            $data['autoTime'] = date("Y-m-d H:i:s");
            if($data['status']==1){
                $data['accountId'] = array_unique($data['accountId']);
                $data['accountId'] = ",".implode($data['accountId'],",").",";


                $userWhere['id'] = $detail['account_id'];
                $project_user = PublicService::detailService("project_account",$userWhere);
                // 服务直通车直接添加一条记录
                $data2['phone'] = $data['phone'];
                $data2['uniacid'] = 1;
                $data2['sumbit_id'] =$project_user['id'];
                $data2['pro_id'] = $detail['pro_id'];
                $data2['sumbit_time'] = date("Y-m-d");
                $data2['content'] = $detail['problem_content'];
                $data2['accountId'] = $data['accountId'];
                $data2['addtime'] = time();
                Db::name("project_help")->insert($data2);
            }
            Db::name("project_pro_figure")->where($where)->update($data);


            $black['code'] = 0;
            $black['msg'] = "操作成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }


    //审核视图
    public function detail(){
        $common = new Common();
        $id = $_REQUEST['id'];
        $detail = $common->public_select("project_pro_figure",['id'=>$id])->find();
        if(!empty($detail['images'])){
            $detail['images'] = explode(";",$detail['images']);
        }

        //企业状况
        $condition = [1=>"运行正常企业",2=>"一般困难企业",3=>"特别困难企业",4=>"停产企业"];
        //查询问题类型
        $where['status'] = 1;
        $where['pid'] = 0;
        $where['type'] = 6;
        $problem = $common->public_select("project_type",$where,"id,name")->order("sort desc,id desc")->select();
        $problem = array_column($problem,'name',"id");
        $detail['condition_val'] = empty($condition[$detail['condition_val']])?"":$condition[$detail['condition_val']];
        $detail['problem'] = empty($problem[$detail['problem_id']])?"":$problem[$detail['problem_id']];

        $solution = \think\Config::get("solution");
        $detail['solution'] = empty($solution[$detail['solution']])?"":$solution[$detail['solution']];


        $detail['show_time'] = date("Y-m-d",strtotime($detail['show_time']));

        //查询协调人员信息
        $accountId = explode(",",$detail['accountId']);
        $accountId = array_filter($accountId);
        $accountWhere['id'] = ['in',$accountId];
        $coordinateList = $common->public_select("project_account",$accountWhere,"id,name,dictionariesId")->select();

        foreach ($coordinateList as $k=>$v){
            //查询责任单位
            $dictionariesWhere['id'] = $v['dictionariesId'];
            $dictionaries = $common->public_select("project_dictionaries",$dictionariesWhere)->value("name");
            $coordinateList[$k]['dictionaries'] = empty($dictionaries)?"":$dictionaries;
        }
        $this->assign('coordinateList',$coordinateList);


        $this->assign('detail',$detail);
        return $this->fetch("detail");
    }

    //求助列表
    public function index(){
        $common = new Common();

        $statusArray = [1=>"待审核",2=>"已通过",3=>"已驳回"];
        if (!empty($_GET['type'])) {
            try{
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $start = ($page - 1) * $limit;

                if(!empty($_GET['account'])){
                    $where['b.account_id'] = $_GET['account'];
                }
                if(!empty($_GET['status'])){
                    $where['b.status'] = $_GET['status']-1;
                }

                //获取服务记录
                $where['b.solution'] = 1;
                $list = Db::name("project_pro")->alias("a")
                    ->join("project_pro_figure b","a.id = b.pro_id")
                    ->field("a.title,a.operation,b.content,b.account_id,b.addtime,b.status,b.id,a.id pid")
                    ->where($where)->limit($start,$limit)->order("b.id desc")->select();
                foreach ($list as $k=>$v){
                    $accountWhere['id'] = $v['account_id'];
                    $account = Db::name("project_account")->where($accountWhere)->value("name");
                    $list[$k]['account'] = $account;
                    $list[$k]['status'] = empty($statusArray[$v['status']+1])?"":$statusArray[$v['status']+1];
                }

                $count = Db::name("project_pro")->alias("a")
                    ->join("project_pro_figure b","a.id = b.pro_id")
                    ->where($where)->count("*");

                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            }catch (Exception $e){
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }else{
            //状态

            $this->assign("status", $statusArray);

            //责任人
            $account = $common->public_select("project_account", ['status' => 1], 'id,name')->select();
            $this->assign("account", $account);

            $istype = empty($_GET['istype'])?0:$_GET['istype'];
            $this->assign("istype", $istype);
            return $this->fetch("index");
        }
    }

}